package com.probiz.estore.common.model.customer;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.customer.base.AddressTbl;
import com.probiz.estore.common.model.order.OrderAddress;
import com.probiz.estore.common.model.order.OrderShipment;
import com.probiz.estore.common.model.system.Region;
import com.probiz.estore.system.service.RegionManager;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * Model - Business object
 * This file won't get overwritten.
 */
public class Address extends AddressTbl {

	/*
	 * MY_CONSULT_LIST - 我的收货人地址列表
	 */
	public static enum FilterHqlName{MY_ADDRESS_LIST};

	public boolean getIsDefault(){
		if(this.isDefaultShippingAddress!=null&&this.isDefaultShippingAddress.equals(Constants.FLAG_TRUE))
			return true;
		else
			return false;
	}

	/**
	 * Default Empty Constructor for class Address
	 */
	public Address () {
		super();
	}
	
	/**
	 * Default Key Fields Constructor for class Address
	 */
	public Address (
		 Integer in_addressId
		) {
		super (
		  in_addressId
		);
	}
	
	public String getAddressDigest(){
		String digest="";
		if(address!=null){
			if(address.length()>10){
				digest=address.substring(0,10) + "...";
			}else{
				digest=address;
			}
		}
		return digest;
	}
	
	/**
	 * 组装地址字符串，用于显示和比较
	 * @see OrderShipment - getOrderAddressString
	 * @return
	 * @author pengzhirong
	 */
	public String getAddressString(){
		RegionManager regionManager = (RegionManager)RequestContext.getSpringBeanById("regionManager");
		Region region = regionManager.getById(this.getRegionId());
		StringBuffer addressStringBuffer = new StringBuffer();
		if(null != region){
			addressStringBuffer.append(region.getFullName());
			addressStringBuffer.append(" ");
			addressStringBuffer.append(this.getAddress()!=null?this.getAddress().trim():"");
			addressStringBuffer.append(" ");
			addressStringBuffer.append(this.getAddress2()!=null?this.getAddress2().trim():"");
		}else{
			addressStringBuffer.append(this.getRegionFullName());
			addressStringBuffer.append(" ");
			addressStringBuffer.append(this.getAddress()!=null?this.getAddress().trim():"");
			addressStringBuffer.append(" ");
			addressStringBuffer.append(this.getAddress2()!=null?this.getAddress2().trim():"");
			
		}
		return addressStringBuffer.toString();
	}
	
	/**
	 * 应用传入地址对象的值到自身
	 * @param address
	 */
	public void applyOrderAddress(OrderAddress orderAddress) {
		this.setFirstname(orderAddress.getFirstname());
		this.setAddress(orderAddress.getAddress1());
		this.setTelephone(orderAddress.getPhoneNumber());
		this.setZip(orderAddress.getPostalcode());
		this.setRegionId(orderAddress.getRegionId());
		this.setRegionFullName(orderAddress.getRegionFullName());
		this.setEmail(orderAddress.getEmail());
	}

}
